package com.yuke.cloud.service.omc.controller.frontend;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.wmc.dto.StorageSpecInfoDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.omc.entity.OdsFavorite;
import com.yuke.cloud.service.omc.service.OdsFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/01/13.
*/
@Api(value = "WEB - OdsFavoriteController", tags = "处理OdsFavorite相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/favorite")
public class OdsFavoriteController extends BaseController {
    @Resource
    private OdsFavoriteService odsFavoriteService;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsFavorite", value = "该数据记录的POJO") @RequestBody OdsFavorite odsFavorite) {
        if (odsFavorite!=null&&odsFavorite.getSpecId()!=null&&odsFavorite.getUserId()!=null&&odsFavorite.getAspectType()!=null){
            Integer num = odsFavoriteService.selectCount(odsFavorite);
            if (num > 0){
                return WrapMapper.error("你已经收藏该商品");
            }
        }

        Example example = new Example(OdsFavorite.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",odsFavorite.getUserId());
        List<OdsFavorite> odsFavorites = odsFavoriteService.selectByExample(example);
        if (odsFavorites.size()>30){
            return WrapMapper.error("收藏已达上限");
        }

        int num =  odsFavoriteService.save(odsFavorite);
        if (num > 0){
            return WrapMapper.ok();
        }else{
            return WrapMapper.error();
        }

    }

    @GetMapping("/delete/{favoriteId}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long favoriteId) {
        odsFavoriteService.deleteById(favoriteId);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsFavorite", value = "需要修改的内容") @RequestBody OdsFavorite odsFavorite) {
        odsFavoriteService.update(odsFavorite);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsFavorite> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsFavorite odsFavorite = odsFavoriteService.findById(id);
        return WrapMapper.ok(odsFavorite);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsFavorite>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsFavorite> list = odsFavoriteService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/getFavorite")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<StorageSpecInfoDto>> favoriteList(@RequestBody JSONObject jsonObject) {
        if (PublicUtil.isEmpty(jsonObject) || PublicUtil.isEmpty(jsonObject.get("userId")) || PublicUtil.isEmpty(jsonObject.get("shopId")) ){
            return WrapMapper.error("传入参数错误！");
        }
       Integer page = jsonObject.getInteger("page");
       Integer size = jsonObject.getInteger("size");
       Long userId = jsonObject.getLong("userId");
       Byte aspectType = jsonObject.getByte("aspectType");
       Long shopId = jsonObject.getLong("shopId");
       //1为用户端
       Byte a = 1;
       //2为商家端
       Byte b = 2;

        PageHelper.startPage(page, size);
        OdsFavorite odsFavorite = new OdsFavorite();
        odsFavorite.setUserId(userId);
        odsFavorite.setAspectType(aspectType);
        List<OdsFavorite> list = odsFavoriteService.select(odsFavorite);
        List<StorageSpecInfoDto> storageSpecInfoDtos=new ArrayList<>();
        if (PublicUtil.isNotEmpty(list) && list.size()>0){
            List<Long> specIds = list.stream().map(OdsFavorite::getSpecId).collect(Collectors.toList());
            //List<StorageSpecInfoDto> storageSpecInfoDtos = wmcStorageFeignApi.getProdStorageSpecBySpecIds(specIds,shopId);
            // storageSpecInfoDtos = wmcStorageFeignApi.getProdSpecInfo(specIds);
            storageSpecInfoDtos = wmcStorageFeignApi.getAllStorageInfoBySpecIds(specIds,shopId);
            // storageSpecInfoDtos = wmcStorageFeignApi.getProdStorageSpecBySpecIds(specIds,shopId);
            //storageSpecInfoDtos = wmcStorageFeignApi.getStorageInfoBySpecIds(specIds,shopId);
            //storageSpecInfoDtos = wmcStorageFeignApi.getWmsSpecInfoBySpecIds(specIds,shopId);

            for (OdsFavorite favorite : list) {
                for (StorageSpecInfoDto storageSpecInfoDto : storageSpecInfoDtos) {
                    if (favorite.getSpecId().equals(storageSpecInfoDto.getSpecId())){
                        storageSpecInfoDto.setFavoriteId(favorite.getFavoriteId());
                        //1为用户端
                        if (favorite.getAspectType().equals(a)){
                            storageSpecInfoDto.setSalePrice(storageSpecInfoDto.getMarketPrice());
                        //2为商家端
                        }else if (favorite.getAspectType().equals(b)){
                            storageSpecInfoDto.setSalePrice(storageSpecInfoDto.getStockPrice());
                        }
                    }
                }
            }
            PageInfo pageInfo = new PageInfo(storageSpecInfoDtos);
            return WrapMapper.ok(pageInfo);
        }
        PageInfo pageInfo = new PageInfo(storageSpecInfoDtos);

       //return WrapMapper.error("未找到收藏记录！");
       return WrapMapper.ok(pageInfo);

    }

    /**
     * 查看商品是否收藏
     * @param odsFavorite
     * @return
     */
    @PostMapping("/findFavoriteGoods")
    public Wrapper<OdsFavorite> findFavoriteGoods(@RequestBody OdsFavorite odsFavorite){

        if (odsFavorite!=null){
            odsFavorite.setNum(0);

            Example example = new Example(OdsFavorite.class);
            Example.Criteria criteria = example.createCriteria();

            criteria.andEqualTo("userId", odsFavorite.getUserId());
            criteria.andEqualTo("specId", odsFavorite.getSpecId());
            criteria.andEqualTo("aspectType", odsFavorite.getAspectType());

           List<OdsFavorite> odsFavoriteList = odsFavoriteService.selectByExample(example);
           if (odsFavoriteList != null && odsFavoriteList.size() > 0){
               odsFavorite.setNum(odsFavoriteList.size());
               odsFavorite.setFavoriteId(odsFavoriteList.get(0).getFavoriteId());
           }

        }else {
            return WrapMapper.error("参数异常");
        }

        return WrapMapper.ok(odsFavorite);
    }


}
